
//initialization
function keyboardInputInit(){
	
	//last focus
	window.lastFruit = null;
	window.lastGoal = null;
	window.lastPremise = null;
	window.lastArgument = null;
	window.lastNode = null;
	
	//track mouse position
	window.mouseX = null;
	window.mouseY = null;
	window.oldMouseX = null;
	window.oldMouseY = null;
	
	//keyboard listener attached to body.  For when no node is selected.
	handlers.hotkeysGlobal = function(event) { return hotkeysGlobal(event) };
		document.addEventListener('keydown', handlers.hotkeysGlobal, false);
		
	//keep track of mouse position (for dimming buttons on movement)
	handlers.mousePos =  function (event){mousePos(event) };
		document.addEventListener('mousemove', handlers.mousePos, false );
	

		
		
}//end initialization


	
//event handlers////////////////////


Node.prototype.onMouseUp = function(e){
    //this.save();  //will enable later
    return false;
}

Node.prototype.onDivClick = function(e){
  	this.editField.focus();
    //I'd like to be able to set the cursor at the end when selected
}

Node.prototype.onFocus = function(e){	
	lastNode = this;
	switch (this.id.substring(0,1)){
		case "F": lastFruit = this;
			break;
		case "G": lastGoal = this;
			break;
			
		case "P": lastPremise = this;
			break;
			
		case "A": lastArgument = this;
			break;
	}
	
	this.startTyping();
}

Node.prototype.onBlur = function(){
	var self = this;
	
	this.displayedDiv.classList.remove("typing");
	typing = false;
    this.displayedDiv.removeEventListener('mousemove', function (event){didItMove(event, self, self.movedMouse)}, false);
	
	this.editField.innerText = $.trim(self.editField.innerText);
}

Node.prototype.onKeyUp= function(){
    this.dirty = true;
   // this.saveSoon();  will enable later
}


//try to set focus to 1.left sibling 2.right sibling 3.parent 4.null
Node.prototype.setNewFocus = function(){
    if(this.parent){//if argument (only arguments have an argument tree parent)
    	//if left sibling
        if(this.parent.children[this.indexToParent -1]){
        	this.parent.children[this.indexToParent -1].editField.focus();
        	lastArgument = this.parent.children[this.indexToParent -1];
        }else{//if right sibling
        	if(this.parent.children[this.indexToParent + 1]){
        		this.parent.children[this.indexToParent + 1].editField.focus();
        		lastArgument = this.parent.children[this.indexToParent + 1];
        	}else{//if no siblings
		        this.parent.editField.focus();
		        lastArgument = this.parent; 
        	}
        }
    }else{// if goal or premise (don't have argument tree parent)
    	//a quick and dirty way to get the index to parent of the closing node and set focus to
    		//the sibling to the left of it, if it exists.  If not, then sibling to the right.  If none there,
    		//then do nothing
    	var index;
    	var i =0;
    	while(this.editField !== this.DOMParent.children[i].childNodes[1]){
	    			index =i;
	    			i++;
		}
		//if left sibling
		if(this.DOMParent.children[(i-1)].childNodes[1]){
			    	this.DOMParent.children[(i-1)].childNodes[1].focus();
			    	//if goal
			    	if(this.DOMParent.id == "goalsContainer"){
			    		lastGoal = this.DOMParent.children[(i-1)].childNodes[1];
			    	}else{//if premise
			    		lastPremise = this.DOMParent.children[(i-1)].childNodes[1];
			    	}
		}else{//if right sibling
			if(this.DOMParent.children[(i+1)]){
				    	this.DOMParent.children[(i+1)].childNodes[1].focus();
				    	//if goal
				    	if(this.DOMParent.id == "goalsContainer"){
				    		lastGoal = this.DOMParent.children[(i+1)].childNodes[1];
				    	}else{//if premise
				    		lastPremise = this.DOMParent.children[(i+1)].childNodes[1];
				    	}
			}else{//if no sibling (was last one)
				//if goal
				if(this.DOMParent.id == "goalsContainer"){
				    		lastGoal = null;
				    	}else{//if premise
				    		lastPremise = null;
				    	}
			}
		}
    }//end outermost ELSE
}//end function


//keyboard shortcuts/hotkeys
Node.prototype.hotkeys = function(e)
{
    switch(e.keyCode){
    	//enter
    	case 13:e.preventDefault();
				if(this.DOMParent == goalsContainer){
					newGoal();
					break;
				}
				if(this.DOMParent == premisesContainer){
					newPremise();
					break;
				}
				
    			if(e.shiftKey){
    				if(e.ctrlKey){
    					this.spawnChild("attack");  //if holding down shift and ctrl
    					break;
    				}
    				this.spawnChild("support"); //if just shift
    				break;
    			}
    			if(e.ctrlKey){
    				this.spawnChild("evidence");
    				break;
    			}
    			if(this.parent){
    				this.parent.spawnChild("support");
    			}
    			break;
    	//up arrow
    	case 38:if(this.parent && e.shiftKey){
        			this.parent.editField.focus();
		    	} 
		    	break;
		//down arrow
		case 40:if(this.children[0]&& e.shiftKey){
					//go to center child
					this.children[Math.floor(this.children.length/2)].editField.focus();
				}
				break;
		//right arrow
		case 39:if(this.parent){
					if(this.parent.children[this.indexToParent +1]&& e.shiftKey){
						this.parent.children[this.indexToParent +1].editField.focus();
					}
				}
				break;
		//left arrow
		case 37:if(this.parent){
					if(this.parent.children[this.indexToParent -1]&& e.shiftKey){
						this.parent.children[this.indexToParent -1].editField.focus();
					}
				}
				break;
    	//backspace
    	case 8:
    	//delete
    	case 46:if(e.shiftKey && ( this !== claim)){ //if shift pressed, close, otherwise consider normal typing
    				this.close();
    				break;
    			}
    			this.startTyping();
    			
    			updateHeight(this);
    			
    			break;
    	//escape
    	case 27:this.editField.blur();
    			break;
    	//tab - cycles between last selected nodes of goals, premises, and arguments
    	case 9:	e.preventDefault();
    				switch(this.DOMParent.id){
    				case "goalsContainer": 	if (lastPremise){
					        					lastPremise.editField.focus();
					        				}else{
					        					lastArgument.editField.focus();
					        				}
    										break;
    				case "premisesContainer": 	if(lastArgument){
    												lastArgument.editField.focus();
    											}
    											break;
    				case "argumentsContainer": 	if(lastGoal){
							        				lastGoal.editField.focus();
						        				}else{
						        					if(lastPremise){
						        						lastPremise.editField.focus();
						        					}
						        				} 
    											break;
    			
    			
    			}//end of TAB switch
    			break;
    	//'q'
    	case 81: if (e.ctrlKey){
    				newGoal();
    			}
    			break;
    	//'a'
    	case 65: if (e.ctrlKey){
    				newPremise();
    			}
    			break;
    	//'z'
    	case 90:if (e.ctrlKey){
    				lastArgument.spawnChild('support');
    			}
    			break;
    			
    	default: this.startTyping();
    			updateHeight(this);
    }//end of outer switch
}

function hotkeysGlobal(e){
	
	//if no node focused
	if(document.activeElement === document.body){
	    switch(e.keyCode){
	    	//backspace
	    	case 8:
	    	//delete
	    	case 46: e.preventDefault();
	    			break;
	    	//escape
	    	case 27: break;  //so that hitting escape to blur doesn't just automatically refocus
	    	//'q'
	    	case 81: if (e.ctrlKey){
	    				newGoal();
		    			break;
	    			}
	    	//'a'
	    	case 65: if (e.ctrlKey){
	    				newPremise();
		    			break;
	    			}
	    	//'z'
	    	case 90: if (e.ctrlKey){
	    				lastArgument.spawnChild('support');
		    			break;
	    			}
	    			
	    	default: if (! e.ctrlKey){  //FIX make this "if not alphanumeric key"
	    				lastNode.editField.focus();  //pick up editing last focused node
	    			 }
	    }//end switch
	}//end if
}
